package wangjg.sso;

import java.io.*;
import java.util.Properties;

/**
 * 
 * 配置
 */
public class Config {

	public final static Config instance = new Config();

	private Properties settings = new Properties();

	private Config() {
		loadConfig();
	}
	
	private void loadConfig() {
		String filePath = "/sso.properties";
		InputStream is = getClass().getResourceAsStream(filePath);
		try {
			int start = filePath.lastIndexOf(".");
			if (filePath.substring(start).toLowerCase().equals(".xml")) {
				settings.loadFromXML(is);
			} else {
				settings.load(is);
			}
		} catch (Exception e) {
			System.err.println("can't read configuration. " + filePath
					+ "make sure the configuration file is in the CLASSPATH.");
			e.printStackTrace();
			return;
		}finally{
			try{if(is!=null)is.close();}catch(Exception e){e.printStackTrace();}
		}
	}

	public Properties getSettings() {
		return settings;
	}

	public String getSetting(String key) {
		return settings.getProperty(key);
	}

	public int getInt(String key) {
		return Integer.parseInt(getSetting(key));
	}

	public long getLong(String key) {
		return Long.parseLong(getSetting(key));
	}

	public float getFloat(String key) {
		return Float.parseFloat(getSetting(key));
	}

	public boolean getBoolean(String key) {
		return Boolean.parseBoolean(getSetting(key));
	}

}